package com.yc.jpyy.common.controller;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class NewASE256 {

	  public static String aesEncrypt(String str, String key) throws Exception {
	        if (str == null || key == null) return null;
	        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
	        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
	        byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
	        return new BASE64Encoder().encode(bytes);
	    }

	    public static String aesDecrypt(String str, String key) throws Exception {
	        if (str == null || key == null) return null;
	        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
	        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
	        byte[] bytes = new BASE64Decoder().decodeBuffer(str);
	        bytes = cipher.doFinal(bytes);
	        return new String(bytes, "utf-8");
	    }
	    
	    public static void main(String[] args) {
			 
			String password = "1";
			String key = "12345678901234567890123456789012";
			System.out.println("明文：" + password);
			System.out.println("key：" + key);
			
			try {
				String str = NewASE256.aesEncrypt(password, key);
				System.out.println("str：" + str);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}

}
